package com.lenovo.smarttraffic.ui.activity;


import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.api.UserManagerClick;
import com.lenovo.smarttraffic.bean.LitepalBean.DBUserCollectBean;
import com.lenovo.smarttraffic.bean.LitepalBean.DBUserInfoBean;
import com.lenovo.smarttraffic.ui.adapter.MyUserInfoRecyclerAdapter;
import com.lenovo.smarttraffic.view.SlideRecyclerView;

import org.litepal.LitePal;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class UserMangerActivity extends BaseActivity {
    private static final String TAG = "UserMangerActivity";
    private SlideRecyclerView mUserInfoRecyclerView;
    private SimpleDateFormat mSimpleDateFormat;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSimpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.CHINA);
        initView();

    }

    private void initView() {
        initToolBar("用户中心");
        mUserInfoRecyclerView = findViewById(R.id.userInfoRecyclerView);
        initRecylerView();
    }

    private void initRecylerView() {
        mUserInfoRecyclerView.setLayoutManager(new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false));
        List<DBUserInfoBean> userInfo = LitePal.findAll(DBUserInfoBean.class);
        MyUserInfoRecyclerAdapter myUserInfoAdapter = new MyUserInfoRecyclerAdapter(userInfo, this);
        myUserInfoAdapter.setUserManagerCllick(new UserManagerClick() {
            @Override
            public void imageClick(View v) {
                startActivity(new Intent(UserMangerActivity.this, UserInfoDetailActivity.class));
            }

            @Override
            public void detailClick(View v) {
                startActivity(new Intent(UserMangerActivity.this, UserCollectActivity.class));
            }

            @Override
            public void collectClick(View view, int pos) {
                List<DBUserInfoBean> mlist = myUserInfoAdapter.getMlist();
                DBUserInfoBean dbUserInfoBean = mlist.get(pos);
                int count=0;
                List<DBUserCollectBean> all = LitePal.findAll(DBUserCollectBean.class);
                if (all.size() != 0) {
                    //如果已有该名字记录则不保存
                    for (DBUserCollectBean item : all) {
                        if (item.getUserName().equals(dbUserInfoBean.getUsername())) {
                            count++;
                        }
                    }
                    if(count==0){
                        saveBean(dbUserInfoBean);
                    }
                    ((ConstraintLayout) view.getParent()).scrollTo(0, 0);
                } else {
                    saveBean(dbUserInfoBean);
                    ((ConstraintLayout) view.getParent()).scrollTo(0, 0);
                }

            }
        });
        mUserInfoRecyclerView.setAdapter(myUserInfoAdapter);
        mUserInfoRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                outRect.top = 20;
            }
        });
    }

    private void saveBean(DBUserInfoBean dbUserInfoBean) {
        DBUserCollectBean dbUserCollectBean = new DBUserCollectBean(dbUserInfoBean.getUsername(), dbUserInfoBean.getPname()
                , dbUserInfoBean.getPtel(), dbUserInfoBean.getPsex(), mSimpleDateFormat.format(new Date()),System.currentTimeMillis(), 0);
        dbUserCollectBean.save();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_user_manger;
    }
}

